/*
 * @Description:工具类
 * @Author: HYH
 * @LastEditors: HYH
 * @LastEditTime: 2022-08-31 10:55:18
 */
import cookies from 'js-cookie'
import changeTheme from './changeTheme'
const Tools = {
  Router: {},
  Store: {},
  LocalStorage: {
    setItem(key: string, value: any) {
      localStorage.setItem(key, JSON.stringify(value))
    },
    getItem(key: string) {
      const value = localStorage.getItem(key) as string
      try {
        return JSON.parse(value)
      } catch (error) {
        return value
      }
    },
    removeItem(key: string) {
      localStorage.removeItem(key)
    },
    clear() {
      localStorage.clear()
    }
  },
  Cookie: {
    setItem(key: string, value: any) {
      cookies.set(key, value, { expires: 30 }) // expires==> 设置cookie过期时间
    },
    getItem(key: string, defaultValue = '') {
      const value = cookies.get(key) || defaultValue
      try {
        return JSON.parse(value)
      } catch (error) {
        return value
      }
    },
    removeItem(key: string) {
      cookies.remove(key)
    }
  },
  Time: {},
  Dom: {},
  /**切换主题 */
  changeTheme,
  /**复制文字到剪贴板 */
  copy: (text: string) => {
    try {
      navigator.clipboard.writeText(text).then(() => ElMessage.success('复制成功'))
    } catch (error) {
      const input = document.createElement('input')
      input.value = text
      document.body.appendChild(input)
      input.select()
      document.execCommand('Copy')
      document.body.removeChild(input)
      ElMessage.success('复制成功')
    }
  }
}
export type ITools = typeof Tools
export default Tools
